package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.text.TextUtils
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.WorkTeamListAdapter
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.WorkTeamActivityDelegate
import cc.vv.btpartyjob.partymanager.moudel.bean.TeamWorkObj
import cc.vv.btpartyjob.partymanager.moudel.resp.GetTeamWorkResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 工作队
 * Created by Chen.qi on 2018/10/29.
 */
class WorkTeamActivity : BaseActivityMVP<WorkTeamActivityDelegate>()
        , BaseQuickAdapter.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener {

    /**
     * 网格适配器
     */
    private var mAdapter: WorkTeamListAdapter? = null

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<TeamWorkObj>? = null

    /**
     *队伍类型： 1 村两委班子 2 扶贫工作队 3 驻村干部
     */
    private var type: Int? = 0

    private var id: String? = null


    override fun initData() {
        type = intent.getIntExtra("type", 0)
        id = intent.getStringExtra("id")
        when (type) {
            1 -> viewDelegate?.setTopBarTitle("村两委班子")
            2 -> viewDelegate?.setTopBarTitle("扶贫工作队")
            3 -> viewDelegate?.setTopBarTitle("驻村干部")
        }

        mDataList = ArrayList()
        if (mAdapter == null) {
            mAdapter = WorkTeamListAdapter(R.layout.item_team_work)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }
        PartyManagerServer.getTeanWorkUrl("$type", id)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {

            }
        })
        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
    }

    override fun getDelegateClass(): Class<WorkTeamActivityDelegate> {
        return WorkTeamActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val intent = Intent(this, GongZuoDuiTeamActivity::class.java)
        intent.putExtra("id", mDataList?.get(position)?.id)
        intent.putExtra("type", type)
        startActivity(intent)
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is GetTeamWorkResponseObj -> {
                viewDelegate?.stopRefreshing()
                if (null == obj.data) {
                    return
                }
                mDataList?.clear()
                if (null != obj.data) {
                    mDataList!!.addAll(obj.data)
                }
                viewDelegate?.setAdapterData(mAdapter, mDataList)
            }
        }

    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        viewDelegate?.stopRefreshing()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        viewDelegate?.stopRefreshing()
    }


    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        PartyManagerServer.getTeanWorkUrl("$type", id)
    }
}